import React from 'react';
import {Spin, Card, Button, Row, Col, Table, Form} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {FORM_OPTIONS, TABLE, DATETIME_FORMAT} from '../constants';
import {improveResultData, getDetail} from './api';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {exceptionInfoTransferApproveResult} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {injectIntl} from './intl';
import styles from './style.css';
import Return from '../common/Return';
import {newOrImproveResultOrderValidator} from './validator';
const FormItem = Form.Item;

class ImproveResultPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        isValidate: false,
        data: {},
        confirmResult: [],
        improveResult: [],
    }
    componentDidMount() {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                        confirmResult: res.data.confirmResult,
                        improveResult: res.data.improveResult,
                    });
                else
                    this.setState({loading: false});
            });
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        const data = this.state.data;
        const isValid = newOrImproveResultOrderValidator(data);
        if(!isValid) {
            this.setState({
                isValidate: true
            });
            return;
        }
        const condition = {
            improveSuggestion: data.improveSuggestion,
            rowVersion: data.rowVersion,
        };
        this.setState({loading: true});
        improveResultData(this.props.id, condition).then(res => {
            if(res.ok)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {confirmResult, improveResult, data, isValidate, loading} = this.state;
        const confirmResultColumns = [{
            title: formatMessage({
                id: 'improveResultPanel.column.confirmorName',
                defaultMessage: '审核人姓名'
            }),
            dataIndex: 'confirmorName',
        }, {
            title: formatMessage({
                id: 'improveResultPanel.column.confirmTime',
                defaultMessage: '审核时间'
            }),
            dataIndex: 'confirmTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'improveResultPanel.column.confirmComment',
                defaultMessage: '审核意见'
            }),
            dataIndex: 'confirmComment',
        }, {
            title: formatMessage({
                id: 'improveResultPanel.column.approveResult',
                defaultMessage: '审核结果'
            }),
            dataIndex: 'approveResult',
            render: text => conventEnumValueToString(exceptionInfoTransferApproveResult, text)
        }];
        const improveResultColumns = [{
            title: formatMessage({
                id: 'improveResultPanel.column.registerName',
                defaultMessage: '登记人姓名'
            }),
            dataIndex: 'registerName',
        }, {
            title: formatMessage({
                id: 'improveResultPanel.column.registerTime',
                defaultMessage: '登记时间'
            }),
            dataIndex: 'registerTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'improveResultPanel.column.improveSuggestion',
                defaultMessage: '改进建议或意见'
            }),
            dataIndex: 'improveSuggestion',
        }];
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'improveResultPanel.column.common',
                        defaultMessage: '质量反馈信息'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'improveResultPanel.operation.improveSuggestion',
                                        defaultMessage: '改进建议或意见'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.improveSuggestion ? 'error' : null} required>
                                        <TextInput
                                            name="improveSuggestion"
                                            value={data.improveSuggestion}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'improveResultPanel.table.confirmResultdetails',
                            defaultMessage: '历史审核结果'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={confirmResultColumns}
                                rowKey="id"
                                pagination={false}
                                dataSource={confirmResult}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'improveResultPanel.table.improveResultdetails',
                            defaultMessage: '历史改进结果'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={improveResultColumns}
                                rowKey="id"
                                pagination={false}
                                dataSource={improveResult}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card className={styles.optionBtn}>
                        <Button
                            type="primary"
                            onClick={this.onSubmit}
                            loading={loading}>
                            {formatMessage({
                                id: 'improveResultPanel.operation.onSubmit',
                                defaultMessage: '提交'
                            })}
                        </Button>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

ImproveResultPanel.propTypes = {
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    tableSearch,
} from './actions';

const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(null, mapDispatchToProps)(injectIntl(ImproveResultPanel));
